﻿@page "/buspoint"
@attribute [StreamRendering]
@using JT808.GPSTracker.Common

<PageTitle>BusPoint</PageTitle>

<h1>BusPoint(P:@PackageCount D:@DelayPackageCount A:@(((double)DelayPackageCount / PackageCount * 100).ToString("0.00"))% C:@messages?.Length)</h1>

@if (messages == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>DeviceId</th>
                <th>Date</th>
                <th>Latitude</th>
                <th>Longitude</th>
                <th>Velocity</th>
                <th>PackageCount</th>
                <th>DelayPackageCount</th>
                <th>数据包延迟率</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var message in messages)
            {
                <tr>
                    <td>@message.DeviceId</td>
                    <td>@message.Timestamp.ToString("MM-dd HH:mm")</td>
                    <td>@message.Latitude</td>
                    <td>@message.Longitude</td>
                    <td>@message.Velocity.ToString("0.00")</td>
                    <td>@message.PackageCount</td>
                    <td>@message.DelayPackageCount</td>
                    <td>@(((double)message.DelayPackageCount / message.PackageCount * 100).ToString("0.00"))%</td>
                </tr>
            }
        </tbody>
    </table>
}

@code {
    private long PackageCount = 0;
    private long DelayPackageCount = 0;
    private VelocityMessage[]? messages;
    [Inject]
    private IClusterClient? client { get; set; }
    protected override async Task OnInitializedAsync()
    {
        var dispatch = client!.GetGrain<IDispatchGrain>(Guid.Empty);
        messages = (await dispatch.GetAllRunPoint()).OrderBy(m => m.DeviceId).ToArray();
        PackageCount = messages.Sum(m => m.PackageCount);
        DelayPackageCount = messages.Sum(m => m.DelayPackageCount);
    }
}
